﻿using System.Diagnostics;
using System.Linq;
using MRGSP.SurveyMaster.Core;
using MRGSP.SurveyMaster.Data;
using NUnit.Framework;
using MRGSP.SurveyMaster.Service.Utils;
using System;

namespace MRGSP.SurveyMaster.Tests.Data
{
    [TestFixture]
    public class QuestionRepositoryTests : BaseRepoTest
    {
        readonly QuestionRepository repo = new QuestionRepository(new ConnectionFactory());
        readonly SurveyRepository surveyRepo = new SurveyRepository(new ConnectionFactory());

        private long sid;

        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            sid = surveyRepo.Insert("aaa".Survey());
        }

        [Test]
        public void InsertMainQuestion()
        {
            var q = repo.Insert(null, "main", 1, sid, false);
            repo.Delete(q);
        }

        [Test]
        public void InsertChildQuestion()
        {
            var main = repo.Insert(null, "main", 1, sid, false);
            repo.Insert(main, "child", 1, sid, false);
            repo.Delete(main);
        }

        [Test]
        public void GetBySurveyId()
        {
            repo.GetBySurveyId(1);
        }

        [Test]
        public void Delete()
        {
            var s = surveyRepo.Insert("a".Survey());
            var q = repo.Insert(null, "q", 1, s, false);
            surveyRepo.Delete(s);
        }

        [Test]
        public void Get()
        {
            var s1 = surveyRepo.Insert("s1".Survey());
            var q = repo.Insert(null, "halo", 1, s1, false);
            
            var watch = new Stopwatch();
            
            watch.Start();

            for (int i = 0; i < 100; i++)
            {
                repo.Get(q); 
            }
            watch.Stop();
            Console.Out.WriteLine(watch.Elapsed);
            watch.Reset();

            watch.Start();
            for (int i = 0; i < 100; i++)
            {
                var qq = repo.Get(q); 
            }
            watch.Stop();
            Console.Out.WriteLine(watch.Elapsed);

            surveyRepo.Delete(s1);
        }

        [Test]
        public void GetByQuestionId()
        {
            var s1 = surveyRepo.Insert("s1".Survey());
            var q1 = repo.Insert(null, "q1", 1, s1, false);
            var q2 = repo.Insert(q1, "q2", 1, s1, false);
            var q3 = repo.Insert(q2, "q3", 3, s1, false);

            repo.GetByQuestionId(q1).Count().IsEqualTo(3);
            repo.GetByQuestionId(q2).Count().IsEqualTo(2);
            var question3 = repo.GetByQuestionId(q3).Single();

            question3.Id.IsEqualTo(q3);
            question3.ParentId.IsEqualTo(q2);
            question3.Type.IsEqualTo((ControlType)3);
            question3.SurveyId.IsEqualTo(s1);
            question3.Mandatory.IsEqualTo(false);

            surveyRepo.Delete(s1);
        }

        [Test]
        public void Update()
        {
            var q1 = repo.Insert(null, "q1", 1, sid, false);
            repo.Update(q1, "jora", 2, false).IsEqualTo(1);

            var q = repo.Get(q1);
            q.Text.IsEqualTo("jora");
            q.Type.IsEqualTo(ControlType.Textbox);
        }

        [Test]
        public void InsertColumnQuestions()
        {
            var q1 = repo.Insert(null, "q1", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.InsertColumnQuestions(q1, "a", (int)ControlType.GridColumn, true).IsEqualTo(5);
        }

        [Test]
        public void GetParentId()
        {
            var s = surveyRepo.Insert("as".Survey());
            var q1 = repo.Insert(null, "q1", 1, s, false);
            var q2 = repo.Insert(q1, "q2", 1, s, false);
            repo.GetParentId(q2).IsEqualTo(q1);
            surveyRepo.Delete(s);
        }

        [Test]
        public void DeleteByColId()
        {
            repo.DeleteByColumnId(1);
        }

        [Test]
        public void InsertCellQuestions()
        {
            var q1 = repo.Insert(null, "q1", 1, sid, false);
            repo.Insert(q1, "foo", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.Insert(q1, "q2", 1, sid, false);
            repo.InsertColumnQuestions(q1, "a", (int)ControlType.GridColumn, true).IsEqualTo(5);
            var qs = repo.GetByQuestionId(q1).ToList();
            var tree = new TreeBuilder().Build(qs);

            var theColumn = tree.First().Children.First().Children.First();
            repo.InsertCellQuestions(theColumn.Id, "halo", 1).IsEqualTo(5);
        }

        [Test]
        public void GetGrandPaIdByColumnId()
        {
            var s = surveyRepo.Insert("aas".Survey());

            var q1 = repo.Insert(null, "q12", 1, s, false);
            repo.Insert(q1, "foo", 1, s, false);
            repo.Insert(q1, "q2", 1, s, false);
            repo.Insert(q1, "q2", 1, s, false);
            repo.Insert(q1, "q2", 1, s, false);
            repo.InsertColumnQuestions(q1, "a", (int)ControlType.GridColumn, true).IsEqualTo(5);
            var qs = repo.GetByQuestionId(q1).ToList();
            var tree = new TreeBuilder().Build(qs);

            var theColumn = tree.First().Children.First().Children.First();
            repo.GetGrandPaIdByColumnId(theColumn.Id).IsEqualTo(q1);
            surveyRepo.Delete(s);
        }

        [Test]
        public void InsertRow()
        {
            var q1 = repo.Insert(null, "q1", 1, sid, false);
            repo.InsertRow(q1, 1, "a", sid);
        }

    }
}